package com.spark.tags;

import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

/**
 * 
 * @version $Id: ComboBox.java v 1.0 2011-3-19 下午03:45:19 IBM Exp $
 * @author <a href="mailto:njq.niu@hand-china.com">vincent</a>
 */
@SuppressWarnings("unchecked")
public class ComboBox extends TriggerField {
	
	public static final String CLASSNAME_NOT_EDITABLE = "s_ne";
	public static final String PROPERTITY_EDITABLE= "editable";
	
	public static final String DEFAULT_TEMPLATES = "comboBox.ftl";
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	protected String getDefaultClass(XMap view,SparkContext context){
		StringBuilder sb = new StringBuilder(super.getDefaultClass(view, context));
		boolean editable = view.getBoolean(PROPERTITY_EDITABLE, true);
		if(!editable) {
			sb.append(" ").append(CLASSNAME_NOT_EDITABLE);
		}
		return sb.toString();
	}
	
	public void init(XMap view, SparkContext context) throws Exception {
		super.init(view, context);
		if(!view.getBoolean(PROPERTITY_EDITABLE, true)){
			context.addJsonConfig(PROPERTITY_EDITABLE,false);
			context.put(PROPERTITY_READONLY,"readonly=\"readonly\"");
		}		
	}
	
	protected String build(SparkContext context) {
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<div class=\"").append(get(map,"className")).append("\" style=\"width:").append(get(map,"width")).append("px;height:").append(get(map,"height")).append("px;").append(get(map,"style")).append("\" id=\"").append(get(map,"id")).append("\">\r");
		sb.append("    <div>\r");
		sb.append("        <input class=\"s_tf\" stype=\"input\" style=\"width: ").append(get(map,"inputWidth")).append("px;line-height:").append(get(map,"lineHeight")).append("px;height:").append(get(map,"inputHeight")).append("px;\" ").append(get(map,"readOnly")).append(" autocomplete=\"off\" value=\"").append(get(map,"value")).append("\"/>\r");
		sb.append("    </div>\r");
		sb.append("    <div class=\"s_tg s_cbtn\" style=\"height:").append(get(map,"trigerHeight")).append("px\" stype=\"trigger\" hideFocus></div>\r");
		sb.append("    <div class=\"s_popup s_cb_v\" stype=\"popup\" style=\"width:").append(get(map,"width")).append("px;\"></div>\r");
		sb.append("</div>\r");
		sb.append("<script>new Spark.ComboBox(").append(get(map,"config")).append(");</script>\r");
		return sb.toString();
	}

	
}
